// StreamDriverReload.cpp : Defines the entry point for the application.  UserMode Version
//

#include "stdafx.h"

#include <devload.h>

int LoadDriver();
TCHAR szDevKey[] =  L"Drivers\\BuiltIn\\UART1";
TCHAR szCOMPort[] = L"COM1:";


int WINAPI WinMain(HINSTANCE hInstance,
                     HINSTANCE hPrevInstance,
                     LPTSTR     lpCmdLine,
                     int       nCmdShow)
{
	LPCTSTR   wstream;
	BOOL usedCommandLine = true;

	wprintf(L"StreamDriverReload App.\r\n");


	wstream = (LPCTSTR)lpCmdLine;

	DWORD cmdlineLength = wcslen(wstream);

	//Default to this stream
	if (cmdlineLength == 0)
	{
		usedCommandLine = false;
	}
	//Check for index no.
	else if (cmdlineLength == 1)
	{
		BYTE index = (BYTE)lpCmdLine[0] - (BYTE) '0';
		if (index <7)
		{
			//Replace 1 in UART1 with index
			szDevKey[wcslen(szDevKey)-1] = (WCHAR)lpCmdLine[0];
			szCOMPort[wcslen(szCOMPort) - 2] = (WCHAR)lpCmdLine[0];
			usedCommandLine = true;
		}
		//Otherwise default to this stream with index 1
		else
		{
			usedCommandLine = false;
		}
	}
	//Otherwise default to this stream with index 1
	else if (cmdlineLength != 5)
	{
		usedCommandLine = false;
	}
	wprintf(L"Working with registry key: %s\r\n", szDevKey);
	return LoadDriver();
}

int LoadDriver()
{
	//TCHAR szDevKey[]  = _T("Drivers\\UART1"); 
	//Assumes registry keys exist

	/*TCHAR szDefaultString[] = L"";
	REGINI RegIniData[] = { L"DefaultString", 
		(LPBYTE)szDefaultString, 
		sizeof(szDefaultString), 
		REG_SZ };*/

	HANDLE hStr = ActivateDeviceEx( szDevKey, NULL, 0, NULL );
	wprintf(L"StreamDriverLoad App: Device HANDLE  is %X \r\n", hStr);

	if (hStr == ((HANDLE)0))  //-1 Orev
	{
		wprintf(L"StreamDriverReload App %s : Driver Load failed\r\n",szCOMPort);
		 return (int) hStr;
	}
	else 
	{
		wprintf(L"StreamDriverReload App %s : Driver Loaded OK\r\n", szCOMPort);
	}

	return 0;
}

